package com.generator.shop.controller;

import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.generator.shop.entity.Shop;
import com.generator.shop.service.ShopService;

import java.util.Date;
import java.util.Map;

import com.generator.utils.*;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "ShopApi")
@RestController
@RequestMapping("/Shop")
@CrossOrigin
public class ShopController {
    @Autowired
    private ShopService service;

    @Operation(summary = "分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示个数", paramType = "query", dataType = "int")
    })
    @GetMapping(value = "/page")
    public Result page(@RequestParam Map<String,Object> params) {
        PageData<Shop> page = service.Page(params);
        //返回结果集
        return new Result().ok(page);
    }

    /**
     * 添加信息
     */
    @PostMapping("add")
    @Operation(summary = "添加信息")
    public Result add(@RequestBody Shop entity) {
        System.out.println(entity);
        int n = service.add(entity);
        if (n > 0) {
            return new Result().ok();
        } else {
            return new Result().err(400, "添加失败");
        }
    }

    /**
     * 编辑信息
     */
    @PutMapping("update")
    @Operation(summary = "修改信息")
    public Result update(@RequestBody Shop entity) {
        int n = service.update(entity);
        if (n > 0) {
            return new Result().ok();
        } else {
            return new Result().err(400, "编辑失败");
        }
    }


    /**
     * 根据ids删除信息
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ids删除信息")
    public Result del(@PathVariable("id") Long id){
        System.out.println(id);
        int n = service.delete(id);
        if (n > 0) {
            return new Result().ok();
        } else {
            return new Result().err(400, "删除失败");
        }
    }
}
